package org.petstore.web.servlet;

import org.petstore.domain.Account;
import org.petstore.service.AccountService;
import org.petstore.service.impl.AccountServiceImpl;
import org.petstore.web.servlet.util.MyUtil;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

import static org.petstore.web.servlet.util.ServletUtil.LOGIN_FORM;

public class EditAccountServlet extends HttpServlet {
    private static final String EDIT_ACCOUNT_FORM = "/WEB-INF/jsp/account/EditAccountForm.jsp";

    private final AccountService accountService = MyUtil.getApplicationContext().getBean("accountService", AccountService.class);


    String msg; //用于传输的信息
    private final Account user = new Account();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        HttpSession session = request.getSession();
        Account loginUser = (Account) session.getAttribute("loginUser");
//        System.out.println("EditAccountServlet   "+loginUser.getUsername()+"  "+request.getParameter("password"));
//        由于在修改用户信息时,默认用户不能修改自己的用户名,因此用request.getParameter("username")始终获取不到
        if (loginUser.getUsername() == null || request.getParameter("password") == null ||
                loginUser.getUsername().equals("") || request.getParameter("password").equals("")) {
            msg = "用户名或密码为空，请重新输入";
            request.setAttribute("msg", msg);
            request.getRequestDispatcher(EDIT_ACCOUNT_FORM).forward(request, response);
        } else if (!request.getParameter("password").equals(request.getParameter("repeatedPassword"))) {
            msg = "两次密码不一致，请重新输入";
            request.setAttribute("msg", msg);
            request.getRequestDispatcher(EDIT_ACCOUNT_FORM).forward(request, response);
        } else {
            user.setAddress1(request.getParameter("address1"));
            user.setAddress2(request.getParameter("address2"));
            user.setCity(request.getParameter("city"));
            user.setUsername(loginUser.getUsername());
            user.setPassword(request.getParameter("password"));
            user.setPhone(request.getParameter("phone"));
            user.setLanguagePreference(request.getParameter("languagePreference"));
            user.setBannerOption(request.getParameter("myBanner") == "myList");
            user.setCountry(request.getParameter("county"));
            user.setEmail(request.getParameter("email"));
            user.setFavouriteCategoryId(request.getParameter("categoryPreference"));
            user.setFirstName(request.getParameter("firstName"));
            user.setLastName(request.getParameter("lastName"));
            user.setListOption(request.getParameter("myList") == "myList");
            user.setState(request.getParameter("state"));
            user.setStatus(request.getParameter("status"));
            user.setZip(request.getParameter("zip"));
            int count = accountService.editAccountIncludeUsername(user, loginUser.getUsername());

            if (count == -1) {
                msg = "该用户名已注册，请重新输入用户名";
                request.setAttribute("msg", msg);
                request.getRequestDispatcher(EDIT_ACCOUNT_FORM).forward(request, response);
            }
            if (count == 0) {
                msg = "貌似出问题了，请重试";
                request.setAttribute("msg", msg);
                request.getRequestDispatcher(EDIT_ACCOUNT_FORM).forward(request, response);
            }
            msg = "修改信息成功，请重新登录";
            session.setAttribute("loginUser", new Account());
            request.setAttribute("msg", msg);
            request.getRequestDispatcher(LOGIN_FORM).forward(request, response);
        }
    }
}
